﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace FtpWorkerRole.FtpServer
{
    public class ConnectionContext : TableServiceContext
    {
        private CloudStorageAccount _account;
        private readonly string _tableName = "LiveConnect";

        public ConnectionContext(CloudStorageAccount account)
            : base(account.TableEndpoint.AbsoluteUri, account.Credentials)
        {
            _account = account;

            var client = new CloudTableClient(_account.TableEndpoint.AbsoluteUri, _account.Credentials);
            client.CreateTableIfNotExist(_tableName);
        }
        public IQueryable<ConnectionEntity> ConnectionEntities
        {
            get
            {
                return CreateQuery<ConnectionEntity>(_tableName);
            }
        }
        public void AddConnection(ConnectionEntity conn)
        {
            AddObject(_tableName, conn);
            SaveChanges();
        }

        public void DeleteConnection(ConnectionEntity conn)
        {
            DeleteObject(conn);
            SaveChanges();
        }

    }
    public class ConnectionEntity : TableServiceEntity
    {
        public string IP { get; set; }
        public int port { get; set; }

        public ConnectionEntity(string name)
            : base(name, Guid.NewGuid().ToString())
        {
        }

        public ConnectionEntity()
            : base()
        {
        }
    }
}
